package warmachinesystem.presentation.editpart;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;

import warmachineroster.Roster;
import warmachinesystem.ArmyList;
import warmachinesystem.BaseUnit;
import warmachinesystem.Solo;
import warmachinesystem.Unit;
import warmachinesystem.UnitAttachment;
import warmachinesystem.Warcaster;
import warmachinesystem.Warjack;
import warmachinesystem.provider.WarmachineSystemEditPlugin;

public class ArmyListTreeEditPart extends WarmachineGameSystemAbstractTreeEditPart {

	public ArmyListTreeEditPart(ArmyList army, Roster roster) {
		super(army, roster);
	}

	public ArmyList getArmyList() {
		return (ArmyList) getModel();
	}

	@Override
	protected String getText() {
		return getArmyList().getName();
	}

	@Override
	protected Image getImage() {
		String imgKey = "full/obj16/" + getArmyList().getCode() + "_16.png";
		URL imgUrl = (URL) WarmachineSystemEditPlugin.INSTANCE.getImage(imgKey);
		ImageDescriptor id = ImageDescriptor.createFromURL(imgUrl);

		return id.createImage();
	}

	@Override
	protected List<?> getModelChildren() {
		List<List<? extends BaseUnit>> children = new ArrayList<List<? extends BaseUnit>>();
		List<Warcaster> warcasters = new ArrayList<Warcaster>();
		List<Warjack> warjacks = new ArrayList<Warjack>();
		List<Unit> units = new ArrayList<Unit>();
		List<UnitAttachment> ua = new ArrayList<UnitAttachment>();
		List<Solo> solos = new ArrayList<Solo>();

		for (BaseUnit elem : getArmyList().getBaseUnits()) {
			if (elem instanceof Warcaster) {
				warcasters.add((Warcaster) elem);
			} else if (elem instanceof Warjack) {
				warjacks.add((Warjack) elem);
			} else if (elem instanceof Unit) {
				units.add((Unit) elem);
			} else if (elem instanceof UnitAttachment) {
				ua.add((UnitAttachment) elem);
			} else if (elem instanceof Solo) {
				solos.add((Solo) elem);
			}
		}

		if (warcasters.size() > 0)
			children.add(warcasters);
		if (warjacks.size() > 0)
			children.add(warjacks);
		if (units.size() > 0)
			children.add(units);
		if (solos.size() > 0)
			children.add(solos);
		if (ua.size() > 0)
			children.add(ua);

		return children;
	}
}
